//#############################################################################
//#############################################################################
//This source file was written by Mert Dogar. If you have some questions about 
//the source code, please do not hesitate to ask me.
//The code is under GPL.
//Email: mertdogar@gmail.com , http://code.google.com/p/electron-cds
//#############################################################################
//#############################################################################

#include "main.h"
#include "component_base.h"
#include "project.h"

#include <dirent.h>

	Gtk::DrawingArea* myArea;
	Gtk::Window* library_window = 0;
	Gtk::TreeView* m_TreeView;
	Glib::RefPtr<Gtk::TreeStore> m_refTreeModel;
	std::string currentpart="";



	void on_library_add();
	void on_library_cancel();
    //Tree model columns:
  class ModelColumns : public Gtk::TreeModel::ColumnRecord
  {
  public:

    ModelColumns()
    { add(m_col_id); add(m_col_name); }

		Gtk::TreeModelColumn<int> m_col_id;
    	Gtk::TreeModelColumn<Glib::ustring> m_col_name;
  };

  ModelColumns m_Columns;

void on_treeview_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* /* column */);
void library_print_sample( std::string cname);
//creator function of the library window. signals are also bound in this function


void show_library_window(Glib::RefPtr<Gnome::Glade::Xml> refXml)
{
	
	
	refXml->get_widget("library_window_drawingarea",myArea);

	struct dirent *de=NULL;
  	DIR *d=NULL;
	int counter=0;

	
	//gets widgets
	Gtk::Button* btn;
	refXml->get_widget("library_window",library_window);
	refXml->get_widget("library_window_treeview",m_TreeView);
    //Create the Tree model:
    m_refTreeModel = Gtk::TreeStore::create(m_Columns);
    m_TreeView->set_model(m_refTreeModel);



    //Fill the TreeView's model
    Gtk::TreeModel::Row row = *(m_refTreeModel->append());
    row[m_Columns.m_col_id] = 1;
    row[m_Columns.m_col_name] = "Basic Components";

	d=opendir("./library/");
 	 if(d == NULL)
  	{
  	  log("Couldn't open directory");
		  return;
  	}
	while(de = readdir(d))
	{	

		counter++;
		log(de->d_name);
		Gtk::TreeModel::Row childrow = *(m_refTreeModel->append(row.children()));
    	childrow[m_Columns.m_col_id] = 10+ counter;
    	childrow[m_Columns.m_col_name] = de->d_name;

	}
	m_TreeView->remove_all_columns();
	
    m_TreeView->append_column("ID", m_Columns.m_col_id);
    m_TreeView->append_column("Component Name", m_Columns.m_col_name);
    
	
	//Connect signal:
    m_TreeView->signal_row_activated().connect(sigc::ptr_fun(on_treeview_row_activated) );
		
	refXml->get_widget("library_window_add",btn);
	btn->signal_clicked().connect(sigc::ptr_fun(on_library_add));
	refXml->get_widget("library_window_cancel",btn);
	btn->signal_clicked().connect(sigc::ptr_fun(on_library_cancel));	
	//finally show the window
	library_window->show();
}
void on_library_add()
{
	set_part_name(currentpart);
	on_library_cancel();
}
void on_library_cancel()
{
	library_window->hide();
}
//treeview activeted signal function
void on_treeview_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* /* column */)
{
  //get the iterator
  Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path);
	if(iter) //if its valid :)
  {
    Gtk::TreeModel::Row row = *iter;
	  
	//controll, will be delated
    std::cout << "Row activated: ID=" << row[m_Columns.m_col_id] << ", Selected Component="
        << row[m_Columns.m_col_name] << std::endl;
	
	//choose the component. change the value of the variable "part" in main_window
	currentpart = Glib::locale_from_utf8(row[m_Columns.m_col_name]);
	library_print_sample(currentpart);
  }

}

void library_print_sample( std::string cname)
{
	Cairo::RefPtr<Cairo::Context> myContext; //CAiro Context
	
	myContext = myArea->get_window()->create_cairo_context();

	myContext->set_source_rgba(0,0,0,1);
	myContext->paint();	
	

	std::string prefix = "./library/";
	prefix += cname;
	cbase tmp(prefix,cname,tovector2 (30,100));
	tmp.draw(myContext,torgbcolor (-1,-1,-1),1,tovector2 (0,0));

}
